import { ApiResponse } from '@/types/data'
import { User } from '@/types/store'
import { RootThunkAction, UserProfile } from '@/types/store'
import request from '@/utils/http'
// 获取用户点赞等信息
export function getUser(): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user')
    console.log(res)
    dispatch({
      type: 'user/info',
      payload: res.data.data
    })
  }
}
// 获取用户个人基本信息
export const getUserProfile = (): RootThunkAction => {
  return async dispatch => {
    const res = await request.get<ApiResponse<UserProfile>>('/user/profile')
    console.log(res.data.data);
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data
    })
  }
}
// 修改个人信息
export function updateUserProfile(key: string, value: string): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/profile', {
      [key]: value
    })
    // 重新渲染
    dispatch(getUserProfile())
  }
}
// 修改头像并重新获取数据
export function updateUserPhoto(fd: FormData): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/photo', fd)
    // 更改成功之后重新获取数据
    dispatch(getUserProfile())
  }
}